package com.chaobl.goods.catalog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chaobl.goods.catalog.entity.Catalog;
import com.chaobl.goods.catalog.service.ICatalogService;
import com.chaobl.goods.shop.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-04-14
 */
@RestController
@RequestMapping("/catalog/catalog")
public class CatalogController {
    @Autowired
    ICatalogService catalogService;

    @Autowired
    RedisService redisService;

    // 首页主分类 10个模块
    @GetMapping("/list")
    public Object list(Integer enterpriseId, Integer shopId) {
        String key = "/catalog/catalog/list" + enterpriseId + "_" + shopId;
        Object obj = redisService.get(key);
        if(obj != null)
            return obj;

        List list = catalogService.list(new QueryWrapper<Catalog>().eq("enterpriseId", enterpriseId)
                .eq("shopId", shopId).eq("pid", 0));

        if(list != null && list.size() > 0)
            redisService.set(key, list);

        return list;

    }
}
